/* global window */
import modelExtend from "dva-model-extend";
import { config } from "utils";
import { query, create, remove, update } from "services/user";

import { pageModel } from "./common";

const { prefix } = config;

export default modelExtend(pageModel, {
  namespace: "user",

  state: {
    currentItem: {},
    modalVisible: false,
    modalType: "create",
    selectedRowKeys: [],
    isMotion: window.localStorage.getItem(`${prefix}userIsMotion`) === "true"
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === "/app/user") {
          dispatch({
            type: "query",
            payload: location.query
          });
        }
      });
    }
  },

  effects: {
    *query({ payload = {} }, { call, put }) {
      payload.type = 1;
      const data = yield call(query, payload);
      if (data) {
        yield put({
          type: "querySuccess",
          payload: {
            list: data.page.list,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.page.totalCount
            }
          }
        });
      }
    },

    *delete({ payload }, { call, put, select }) {
      const data = yield call(remove, { id: payload });
      const { selectedRowKeys } = yield select(_ => _.user);
      if (data.success) {
        yield put({
          type: "updateState",
          payload: {
            selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload)
          }
        });
        yield put({ type: "query" });
      } else {
        throw data;
      }
    },

    *multiDelete({ payload }, { call, put }) {
      const data = yield call(remove, payload);
      if (data.success) {
        yield put({ type: "updateState", payload: { selectedRowKeys: [] } });
        yield put({ type: "query" });
      } else {
        throw data;
      }
    },

    *create({ payload }, { call, put }) {
      const data = yield call(create, payload);
      if (data.success) {
        yield put({ type: "hideModal" });
        yield put({ type: "query" });
      } else {
        throw data;
      }
    },

    *update({ payload }, { select, call, put }) {
      const id = yield select(({ user }) => user.currentItem.id);
      const newUser = { ...payload, id };
      const data = yield call(update, newUser);
      if (data.success) {
        yield put({ type: "hideModal" });
        yield put({ type: "query" });
      } else {
        throw data;
      }
    }
  },

  reducers: {
    showModal(state, { payload }) {
      return { ...state, ...payload, modalVisible: true };
    },

    hideModal(state) {
      return { ...state, modalVisible: false };
    },

    switchIsMotion(state) {
      window.localStorage.setItem(`${prefix}userIsMotion`, !state.isMotion);
      return { ...state, isMotion: !state.isMotion };
    }
  }
});
